local Msg = require("core.Msg")
local Util = require("common.Util")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local OpenAct = require("present.OpenAct")
local YunYingLogic = require("yunying.YunYingLogic")
local OpenActExcel = require("excel.openAct")
local BuyExcel = require("excel.buy")
--[[
	开服活动-单笔充值
	DB:
		human.db.openServerSingleCharge = {
			[id] = {		  	-- 档位id
				cnt = xxx,    	-- 已领取次数
				activeCnt = xxx,-- 已激活次数
			},
			...
		}
	local:
		getDB()				  	-- 获得单笔充值DB数据
        getDayLeftTime()        -- 获得当天剩余时间秒
		wrapSChargeList()		-- 包装单笔充值档位数据
	public:
		isRed()                 -- 红点提醒
		isActive()				-- 激活状态
		isBaseOpen()			-- 开启活动基本条件	
		isOpen()                -- 活动开启	
		getLeftTime()           -- 得到活动剩余时间	
		query()					-- 发送活动信息
		get()					-- 领取活动奖励
		onCharge()				-- 充值回调
		updateDaily()		    -- 每日重置
--]]

STATE_0 = 0	--不可领
STATE_1 = 1 --可领
STATE_2 = 2 -- 已领完

local function getDB(human)
	human.db.openServerSingleCharge = human.db.openServerSingleCharge or {}
	return human.db.openServerSingleCharge
end

local function getDayLeftTime()
	local now = os.time()
 	local endTime = Util.getDayStartTime(now) + 86400
    
    return endTime - now
end

local function wrapSChargeList(net, id, sChargeInfo, sChargeDB)
	net.id = id
	net.needPrice = BuyExcel.buy[sChargeInfo.needBuyId].CN
	net.maxCnt = sChargeInfo.maxCnt
	net.cnt = sChargeDB[id] and sChargeDB[id].cnt or 0

	if sChargeDB[id] then
		if sChargeDB[id].activeCnt > sChargeDB[id].cnt then
			net.state = STATE_1
		elseif sChargeDB[id].cnt >= sChargeInfo.maxCnt then
			net.state = STATE_2
		else
			net.state = STATE_0
		end
	else
		net.state = STATE_0
	end
	
 	local len = 0
	for index,itemInfo in ipairs(sChargeInfo.rewards) do
		len = len + 1
		Grid.makeItem(net.items[index], itemInfo[1], itemInfo[2])
	end
	net.items[0] = len
end

function isRed(human)
	local sChargesDB = human.db.openServerSingleCharge
	if not sChargesDB then return end

    for id in ipairs(OpenActExcel.singleCharge) do
       if sChargesDB[id] and sChargesDB[id].activeCnt > sChargesDB[id].cnt then return true end
    end
end

function isActive(human, YYInfo, funcConfig)
    return not isOpen(human, YYInfo, funcConfig)
end

function isBaseOpen(human, YYInfo, funcConfig, noSend)
    if human.db.lv < funcConfig.openLv then
        if not noSend then
            local str = Util.format(Lang.ROLE_LEV_ERROR, funcConfig.openLv)
            return Broadcast.sendErr(human, str)
        end
    end

    local flag = OpenAct.getOpenActTime(funcConfig.param)
    if not flag then return end

    return true
end

function isOpen(human, YYInfo, funcConfig)
	if isBaseOpen(human, YYInfo, funcConfig, true) then return true end
end

function getLeftTime(human, YYInfo, funcConfig)
	local _,leftTime = OpenAct.getOpenActTime(funcConfig.param)
 	return leftTime
end

function query(human)
	local sChargeDB = getDB(human)
    local dayLeftTime = getDayLeftTime()

	local msgRet = Msg.gc.GC_OPEN_SERVER_SINGLE_CHARGE_QUERY
	msgRet.leftTime = dayLeftTime

	local len = 0
	for id,sChargeInfo in ipairs(OpenActExcel.singleCharge) do
		len = len + 1
		wrapSChargeList(msgRet.list[len], id, sChargeInfo, sChargeDB)
	end
	msgRet.list[0] = len

	Msg.send(msgRet, human.fd)
end

function get(human, funcID, id)
	local funcConfig = YunYingLogic.getFuncConfig(funcID)
	if not funcConfig then return end

	local sChargeInfo = OpenActExcel.singleCharge[id]	
	if not sChargeInfo then return end

	local sChargeDB = getDB(human)
	if not sChargeDB[id] or sChargeDB[id].cnt >= sChargeDB[id].activeCnt then return end

	sChargeDB[id].cnt = sChargeDB[id].cnt + 1
	BagLogic.addItemList(human, sChargeInfo.rewards, "openServerSCharge_get")

	query(human)

    -- 现在没有红点了通知
    if not isRed(human) then
	    YunYingLogic.sendBanner(human)
        YunYingLogic.updateIcon(YYInfo[funcID], human)
        YunYingLogic.sendGroupUpdate(YYInfo[funcID], human, funcConfig.panelID)
    end
end

function onCharge(human, price, funcID, buyID)
	local funcConfig = YunYingLogic.getFuncConfig(funcID)
	if not funcConfig then return end

    if not isOpen(human, nil, funcConfig) then return end

    -- 取之前是不是有红点
    local beforeRed = isRed(human, nil, funcConfig)

	local sChargeDB = getDB(human)
	local isActive
    for id,sChargeInfo in ipairs(OpenActExcel.singleCharge) do
    	if buyID == sChargeInfo.needBuyId and 
    		(not sChargeDB[id] or sChargeDB[id].activeCnt < sChargeInfo.maxCnt) then
    		sChargeDB[id] = sChargeDB[id] or {cnt=0,activeCnt=0}
    		sChargeDB[id].activeCnt = sChargeDB[id].activeCnt + 1
    		isActive = true
    	end
    end

    -- 之前没有红点,现在激活了红点通知(只需要通知banner上的红点)
    if not beforeRed and isActive then
		YunYingLogic.sendBanner(human)
    end
end

function updateDaily(human, activityID)
	if not human.db.openServerSingleCharge then return end
    human.db.openServerSingleCharge = nil
end